package com.mrd.gtimpframe.service.impl;

import com.mrd.gtimpframe.entity.User;
import com.mrd.gtimpframe.repository.UserMapper;
import com.mrd.gtimpframe.service.UserService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 增加用户角色
     */
    @Override
    @Transactional
    public void addRoles(String userId, String... roles) {
        for (String role : roles) {
            userMapper.insertRole(userId, role);
        }
    }

    /**
     * 给公司下的所有用户增加角色
     */
    @Override
    @Transactional
    public void addRoles4Company(String comId, String... roles) {
        List<User> userList = this.getUsersByCompany(comId);
        if (userList != null) {
            for(User user : userList){
                this.addRoles(user.getuId(),roles);
            }
        }
    }

    @Override
    @Transactional
    public void deleteRoles(String comId, String... roles) {
        List<User> userList = this.getUsersByCompany(comId);
        if (userList != null) {
            for(User user : userList){
                for(String role : roles){
                    this.deleteRole(user.getuId(),role);
                }
            }
        }
    }

    @Override
    public void deleteRole(String userId, String role) {
        userMapper.deleteRole(userId,role);
    }

    @Override
    public List<User> getUsersByCompany(String comId) {
        return userMapper.selectByCompany(comId);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Object id) {
        return userMapper.deleteByPrimaryKey((String) id);
    }

    @Override
    @Transactional
    public int insert(Object o) {
        return userMapper.insert((User) o);
    }

    @Override
    @Transactional
    public int insertSelective(Object o) {
        return userMapper.insertSelective((User) o);
    }

    @Override
    public Object selectByPrimaryKey(Object id) {
        return userMapper.selectByPrimaryKey((String) id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(Object o) {
        return userMapper.updateByPrimaryKeySelective((User) o);
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(Object o) {
        return userMapper.updateByPrimaryKey((User) o);
    }
}
